package com.yj.common.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.Scanner;

/**
 * <p>
 * 	代码生成器请勿轻易操作
 * 	操作时请取消 pom.xml 中的 velocity 注释
 * </p>
 * @author
 */
public class MybatisGenerateCode {

	private static final String baseSrc = "E://YUNJIE/yj/asset/";

	/**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		AutoGenerator mpg = new AutoGenerator();
		// 全局配置
        GlobalConfig gc = new GlobalConfig();
        //生成存放地址
        gc.setOutputDir(baseSrc+"src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(false);
        gc.setAuthor("yj");
        gc.setDateType(DateType.ONLY_DATE);

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root1100");
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/asset?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        mpg.setDataSource(dsc);

        // 包配置
        final PackageConfig pc = new PackageConfig();
        pc.setParent("com.yj.asset");
        //dao
        pc.setMapper("mapper");
        //servcie
		pc.setService("service");
		pc.setServiceImpl("service.impl");
        //controller
		pc.setController("controller");
		pc.setEntity("entity");
        //mapper.xml
		pc.setXml("mapper.mapper");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setRestControllerStyle(true);
        // 此处可以修改为您的表前缀
        strategy.setTablePrefix(new String[] {""});
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //生成指定表
        strategy.setInclude(scanner("请输入表名多个表使用逗号隔开").split(","));
        mpg.setStrategy(strategy);

        //注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】  ${cfg.abc}
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            	Map<String, Object> testMap = Maps.newHashMap();
            	testMap.put("dd", "222");
                this.setMap(testMap);
            }
        };

        mpg.setCfg(cfg);

        // 执行生成
        mpg.execute();
	}
}